import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
// import { Document } from 'mongoose';
import { signToken } from 'src/utils/utils';
import { WxService } from 'src/wx/wx.service';
import { Auth, authModel, profileModel } from './entity/users.entity';
import { Profile, Property } from './interface/profile.interface';

@Injectable()
export class UserService {
  async login(js_code: string) {
    const wxService = new WxService();
    const req = await wxService.getSessionKey(js_code);
    if (req.success) {
      const { openid, session_key } = req;
      const user: Auth = await authModel.findOne({ openid });
      if (user) {
        await authModel.updateOne(
          {
            openid,
          },
          { $set: { session_key } },
        );
      } else {
        await authModel.insertOne({
          openid,
          session_key,
        });
      }
      return signToken({
        openid,
      });
    } else {
      throw new Error(req.errmsg);
    }
  }

  async isEmptyProfile(openid: string) {
    let profile = null;
    try {
      profile = await profileModel.findOne({
        openid,
      });
    } catch (err) {
      throw new HttpException(
        {
          message: '服务器内部错误',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
    if (profile) {
      return false;
    }
    return true;
  }

  /**
   *
   * @public
   */
  public async addProfile(
    openid: string,
    tag: Array<string>,
    nickname: string,
    head_image: string,
    allow_stop: boolean,
  ) {
    let isEmpty = false;
    const property: Property = {
      tomato: 0,
    };
    isEmpty = await this.isEmptyProfile(openid);
    if (!isEmpty) {
      throw new HttpException(
        {
          message: '重复添加',
        },
        HttpStatus.ACCEPTED,
      );
    }
    try {
      await profileModel.insertOne({
        openid,
        tag,
        nickname,
        head_image,
        property,
        allow_stop,
      });
    } catch (err) {
      throw new HttpException(
        {
          message: '服务器内部错误',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }

    return;
  }

  async updateProfile(openid: string, newProfile: Profile) {
    let profile = null;
    const { tag, nickname } = newProfile;
    const isEmpty = await this.isEmptyProfile(openid);
    if (isEmpty) {
      throw new HttpException(
        {
          message: '该用户不存在',
        },
        HttpStatus.FORBIDDEN,
      );
    }
    try {
      profile = await profileModel.updateOne(
        { openid },
        {
          tag,
          nickname,
        },
      );
    } catch (err) {
      throw new HttpException(
        {
          message: '服务器内部错误',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
    return profile;
  }

  /**
   *
   * @public
   */
  public async getProfile(openid: string) {
    let profile: Profile | null = null;
    try {
      profile = await profileModel.findOne<Profile>({ openid });
    } catch (err) {
      throw new HttpException(
        {
          message: '服务器内部错误',
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
    return profile;
  }
}
